/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server.settings;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * The Class WeekDaySchedule.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public final class WeekDaySchedule extends TimeSchedule {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 40L;

	/** The sunday selected. */
	private boolean sundaySelected = false;

	/** The monday selected. */
	private boolean mondaySelected = false;

	/** The tuesday selected. */
	private boolean tuesdaySelected = false;

	/** The wednesday selected. */
	private boolean wednesdaySelected = false;

	/** The thursday selected. */
	private boolean thursdaySelected = false;

	/** The friday selected. */
	private boolean fridaySelected = false;

	/** The saturday selected. */
	private boolean saturdaySelected = false;

	/** The start hour. */
	private int startHour;

	/** The start minute. */
	private int startMinute;

	/**
	 * Instantiates a new week day schedule.
	 *
	 * @param startHour the start hour
	 * @param startMinute the start minute
	 */
	public WeekDaySchedule(int startHour, int startMinute) {
		this.startHour = startHour;
		this.startMinute = startMinute;
	}

	@Override
	public boolean isDateIntoSchedule(Date date) {

		if (date == null) {
			return false;
		}

		final Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);

		final int currentDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

		if (((currentDayOfWeek == Calendar.SUNDAY) && (this.sundaySelected == true)) || ((currentDayOfWeek == Calendar.MONDAY) && (this.mondaySelected == true)) || ((currentDayOfWeek == Calendar.TUESDAY) && (this.tuesdaySelected == true)) || ((currentDayOfWeek == Calendar.WEDNESDAY) && (this.wednesdaySelected == true)) || ((currentDayOfWeek == Calendar.THURSDAY) && (this.thursdaySelected == true)) || ((currentDayOfWeek == Calendar.FRIDAY) && (this.fridaySelected == true)) || ((currentDayOfWeek == Calendar.SATURDAY) && (this.saturdaySelected == true))) {
			if (this.getStartHour() == calendar.get(Calendar.HOUR_OF_DAY)) {
				if (this.getStartMinute() == calendar.get(Calendar.MINUTE)) {
					return true;
				}
			}
		}

		return false;
	}

	@Override
	public boolean isDateIntoSchedule(Date date, Date lastDate) {

		final boolean result = this.isDateIntoSchedule(date);

		if (lastDate == null) {
			return result;
		}

		if (result == false) {
			return false;
		}

		final Calendar calendar = new GregorianCalendar();
		final Calendar lastCalendar = new GregorianCalendar();
		calendar.setTime(date);
		lastCalendar.setTime(lastDate);

		if ((lastCalendar.get(Calendar.YEAR) != calendar.get(Calendar.YEAR)) || (lastCalendar.get(Calendar.MONTH) != calendar.get(Calendar.MONTH)) || (lastCalendar.get(Calendar.DAY_OF_WEEK) != calendar.get(Calendar.DAY_OF_WEEK))) {
			return true;
		}

		return false;
	}

	/**
	 * Gets the start hour.
	 *
	 * @return the start hour
	 */
	public int getStartHour() {
		return this.startHour;
	}

	/**
	 * Gets the start minute.
	 *
	 * @return the start minute
	 */
	public int getStartMinute() {
		return this.startMinute;
	}

	/**
	 * Sets the start hour.
	 *
	 * @param startHour the new start hour
	 */
	public void setStartHour(int startHour) {
		this.startHour = startHour;
	}

	/**
	 * Sets the start minute.
	 *
	 * @param startMinute the new start minute
	 */
	public void setStartMinute(int startMinute) {
		this.startMinute = startMinute;
	}

	/**
	 * Checks if is friday selected.
	 *
	 * @return true, if is friday selected
	 */
	public boolean isFridaySelected() {
		return this.fridaySelected;
	}

	/**
	 * Sets the friday selected.
	 *
	 * @param fridaySelected the new friday selected
	 */
	public void setFridaySelected(boolean fridaySelected) {
		this.fridaySelected = fridaySelected;
	}

	/**
	 * Checks if is monday selected.
	 *
	 * @return true, if is monday selected
	 */
	public boolean isMondaySelected() {
		return this.mondaySelected;
	}

	/**
	 * Sets the monday selected.
	 *
	 * @param mondaySelected the new monday selected
	 */
	public void setMondaySelected(boolean mondaySelected) {
		this.mondaySelected = mondaySelected;
	}

	/**
	 * Checks if is saturday selected.
	 *
	 * @return true, if is saturday selected
	 */
	public boolean isSaturdaySelected() {
		return this.saturdaySelected;
	}

	/**
	 * Sets the saturday selected.
	 *
	 * @param saturdaySelected the new saturday selected
	 */
	public void setSaturdaySelected(boolean saturdaySelected) {
		this.saturdaySelected = saturdaySelected;
	}

	/**
	 * Checks if is sunday selected.
	 *
	 * @return true, if is sunday selected
	 */
	public boolean isSundaySelected() {
		return this.sundaySelected;
	}

	/**
	 * Sets the sunday selected.
	 *
	 * @param sundaySelected the new sunday selected
	 */
	public void setSundaySelected(boolean sundaySelected) {
		this.sundaySelected = sundaySelected;
	}

	/**
	 * Checks if is thursday selected.
	 *
	 * @return true, if is thursday selected
	 */
	public boolean isThursdaySelected() {
		return this.thursdaySelected;
	}

	/**
	 * Sets the thursday selected.
	 *
	 * @param thursdaySelected the new thursday selected
	 */
	public void setThursdaySelected(boolean thursdaySelected) {
		this.thursdaySelected = thursdaySelected;
	}

	/**
	 * Checks if is tuesday selected.
	 *
	 * @return true, if is tuesday selected
	 */
	public boolean isTuesdaySelected() {
		return this.tuesdaySelected;
	}

	/**
	 * Sets the tuesday selected.
	 *
	 * @param tuesdaySelected the new tuesday selected
	 */
	public void setTuesdaySelected(boolean tuesdaySelected) {
		this.tuesdaySelected = tuesdaySelected;
	}

	/**
	 * Checks if is wednesday selected.
	 *
	 * @return true, if is wednesday selected
	 */
	public boolean isWednesdaySelected() {
		return this.wednesdaySelected;
	}

	/**
	 * Sets the wednesday selected.
	 *
	 * @param wednesdaySelected the new wednesday selected
	 */
	public void setWednesdaySelected(boolean wednesdaySelected) {
		this.wednesdaySelected = wednesdaySelected;
	}
}
